package boun.center;

import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Pair;
import android.widget.GridView;

public class RegistrationActivity extends Activity {
	GridView grid;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.final_grid);
		grid = (GridView) findViewById(R.id.final_grid);
		new FetchingTask().execute();
	}
	
	class FetchingTask extends AsyncTask<Void,Void,FinalExamListAdapter>{
		ProgressDialog dialog;
		public FetchingTask(){
			dialog = new ProgressDialog(RegistrationActivity.this);
		}
		@Override
		protected FinalExamListAdapter doInBackground(Void... params) {
			FinalExamListAdapter ca;
			try {
				FileInputStream fis = openFileInput("preferences");
				Pair<String,String> p = readFrom(fis);
				System.out.println(p.first +"------------"+ p.second);
				ca = new FinalExamListAdapter(RegistrationActivity.this,p);
				return ca;
			} catch (IOException e) {
			} catch (Exception e) {
			}
			return null;
		}
		
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(RegistrationActivity.this,"","Fetching..",true);
		}
		@Override
		protected void onPostExecute(FinalExamListAdapter la){
			dialog.dismiss();
			grid.setAdapter(la);
		}
	}
	
	public static Pair<String, String> readFrom(FileInputStream fis) throws IOException {
		byte[] buffer = new byte[1024];
		fis.read(buffer);
		fis.close();
		String m = new String(buffer);
		String id = m.split("\n")[0];
		String pw = m.split("\n")[1];
		return new Pair<String,String>(id,pw);
	}
}
